package com.hxzy.bbs.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hxzy.jsp.dao.PostDAO;
import com.hxzy.jsp.dao.PostDAOImpl;
import com.hxzy.jsp.dao.UserDAOImpl;
import com.hxzy.jsp.entity.Post;
import com.hxzy.jsp.entity.User;
import com.hxzy.test.PostDaoHibernateImpl;
import com.hxzy.test.UserDaoHibernateImpl;

public class TopicServlet extends AppServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Long id = Long.parseLong(request.getParameter("id"));
		PostDAO postDAO = new PostDaoHibernateImpl();
		Post post = postDAO.find(id);
		
		if ( post == null) {
			response.sendRedirect("./error");
			return;
		}
		User postUser = new UserDaoHibernateImpl().find(post.getUserId());
		List<Post> userPosts = postDAO.findUserLatestPosts(post.getUserId(), 5);
		request.setAttribute("postUser", postUser);
		request.setAttribute("post", post);
		request.setAttribute("userPosts", userPosts);
		request.getRequestDispatcher("/WEB-INF/topic.jsp").forward(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
